<?php

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\dispatch\DriverModel;
use App\Models\DispatchTaskModel;
use App\Models\PreTakeModel;
use Illuminate\Http\Request;


class My extends Controller
{

    /**
     * Notes:我的
     * User: xiangzi
     * Date: 2024/10/9
     * Time: 14:14
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request) {
        $auth = $request->get('auth');

        $model = DriverModel::find($auth->id);

        $return = [
            'name' => $model->name,
            'mobile' => $model->mobile,
            'id_number' => $model->id_number,
            'login_mobile' => $model->login_mobile,
        ];

        $pre_take = PreTakeModel::where('driver_id', $auth->id)->where('status', 2)->where('is_read', 0)->get();
        $task = DispatchTaskModel::where(function ($query) use ($auth) {
            $query->where('driver_id', $auth->id)->orwhere('hk_driver_id', $auth->id);
        })->where('status', 1)->get();

        $return['msg_number'] = $pre_take->count() + $task->count();

        return $this->success($return);
    }

    /**账号信息
     * Notes:
     * User: xiangzi
     * Date: 2024/10/9
     * Time: 14:16
     * @return \Illuminate\Http\JsonResponse
     */
    public function account(Request $request)
    {
        $auth = $request->get('auth');

        $model = DriverModel::find($auth->id);

        $login_mobile = $model->login_mobile;

        return $this->success(['login_mobile' => $login_mobile]);

    }

    /**
     * Notes:消息通知
     * User: xiangzi
     * Date: 2024/10/9
     * Time: 14:48
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function msg(Request $request)
    {
        $auth = $request->get('auth');

        $pre_take = PreTakeModel::where('driver_id', $auth->id)->where('status', 2)->where('is_read', 0)->get();

        $pre_take_list = $pre_take->map(function($item) {
            $res = [
                'id' => $item->id,
                'time' => $item->arrange_time ?? ''
            ];
            return $res;
        });

        $task = DispatchTaskModel::where(function ($query) use ($auth) {
            $query->where('driver_id', $auth->id)->orwhere('hk_driver_id', $auth->id);
        })->where('status', 1)->where('is_read', 0)->get();

        $task_list = $task->map(function($item) {
            $res = [
                'id' => $item->id,
                'time' => $item->created_at->toDateTimeString()
            ];
            return $res;
        });

        return $this->success(['pre_take_list' => $pre_take_list, 'task_list' => $task_list]);
    }



}
